package com.smarttest.portal.controller;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Vector;
import java.util.concurrent.atomic.AtomicInteger;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.smarttest.common.beans.GeneralResultWithStatus;
import com.smarttest.portal.pojo.CaseGroupWithBLOBs;
import com.smarttest.portal.service.RunTestService;
@Controller
public class AutotestController {
	@Autowired
	RunTestService runtestservice;
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	/**
	 * 运行java工程的自动化测试(暂时未使用该代码)
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/runtest.action", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public GeneralResultWithStatus runTest (HttpServletRequest request) {
		String level = request.getParameter("level").trim();
		String groups = request.getParameter("groups").trim();
		final String release = request.getParameter("release").trim();
		final String time = sdf.format(new Date()).replaceAll(" ", "%20");
		final String mailers = "duanxiangyun@huoyunren.com";
		List<CaseGroupWithBLOBs> l = runtestservice.getCase(groups);
		Map<Integer,String> s = new HashMap<>();
		for (CaseGroupWithBLOBs g : l) {
			int key = g.getType();
			String value = "";
			if (level.equalsIgnoreCase("level1")) {
				value = g.getLevel1();
			}
			else if (level.equalsIgnoreCase("level2")) {
				value = g.getLevel2();
			}
			else if (level.equalsIgnoreCase("level3")) {
				value = g.getLevel3();
			}
			if (s.containsKey(key)) {
				s.get(key).concat(","+value);
			}
			else {
				s.put(key, value);
			}
		}
		Set<Entry<Integer, String>> i = s.entrySet();
		JSONObject jo = new JSONObject();
		final AtomicInteger count = new AtomicInteger(0);
		final JSONArray ja = new JSONArray();
		 Vector<Thread> threads = new Vector<Thread>();  
		for (final Entry<Integer, String> e : i) {
			Thread t = new Thread(new Runnable() {
				public void run() {
					String runjob = "";
					if (e.getKey() == 1) {
						runjob = "DSP_interfacetest";
					}
					else if (e.getKey() == 2) {
						runjob = "DSP_MessageStorage";
					}
					JSONObject j = runtestservice.runTest(time, e.getValue(), mailers, release, runjob);
					if (j.getIntValue("code") != 0) {
						count.incrementAndGet();
						ja.add(j);
					}
				}
			});
			threads.add(t);
			t.start();
		}
		for (Thread iThread : threads) {  
		      try {  
		        // 等待所有线程执行完毕  
		        iThread.join();  
		      } catch (InterruptedException e) {  
		        e.printStackTrace();  
		      }  
		}  
		jo.put("code", count);
		jo.put("result", ja);
		if (count.get() == 0) {
			jo.put("message", "自动化通过");
		}
		else {
			jo.put("message", "自动化测试存在错误");
		}
		return GeneralResultWithStatus.okData(null, jo) ;
	} 
}
